package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.po.AddressPO;
import com.atguigu.crowd.entity.po.AddressPOExample;
import com.atguigu.crowd.entity.po.OrderPO;
import com.atguigu.crowd.entity.po.OrderProjectPO;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.entity.vo.OrderVO;
import com.atguigu.crowd.mapper.AddressPOMapper;
import com.atguigu.crowd.mapper.OrderPOMapper;
import com.atguigu.crowd.mapper.OrderProjectPOMapper;
import com.atguigu.crowd.service.api.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderProjectPOMapper orderProjectPOMapper;

    @Autowired
    private AddressPOMapper addressPOMapper;

    @Autowired
    private OrderPOMapper orderPOMapper;

    //确认回报内容页 查询项目信息
    @Override
    public OrderProjectVO getOrderProjectVO(Integer projectId, Integer returnId) {
        return orderProjectPOMapper.selectOrderProjectVO(returnId);
    }

    //确认订单页 查询收获地址
    @Override
    public List<AddressVO> getAddressVOList(Integer memberId) {
        AddressPOExample example = new AddressPOExample();

        example.createCriteria().andMemberIdEqualTo(memberId);

        List<AddressPO> addressPOList = addressPOMapper.selectByExample(example);

        List<AddressVO> addressVOList = new ArrayList<AddressVO>();

        for (AddressPO addressPO : addressPOList) {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressPO, addressVO);

            addressVOList.add(addressVO);
        }

        return addressVOList;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveAddress(AddressVO addressVO) {
        AddressPO addressPO= new AddressPO();
        BeanUtils.copyProperties(addressVO,addressPO);
        addressPOMapper.insert(addressPO);
    }

    //保存订单信息 要保存t_order和t_order_project
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveOrder(OrderVO orderVO) {
        OrderPO orderPO= new OrderPO();
        BeanUtils.copyProperties(orderVO,orderPO);

        OrderProjectPO orderProjectPO=new OrderProjectPO();

        BeanUtils.copyProperties(orderVO.getOrderProjectVO(),orderProjectPO);

        //存order
        orderPOMapper.insert(orderPO);


        //到OrderPOMapper.xml设置 获取主键
        //  <insert id="insert"  parameterType="com.atguigu.crowd.entity.po.OrderPO" useGeneratedKeys="true" keyProperty="id">

        //拿到order的自增主键 用于在保存orderProjectPO时使用
        Integer id = orderPO.getId();

        orderProjectPO.setOrderId(id);
        System.out.println("获得的订单的ID "+id);
        System.out.println("orderProjectPO获得的订单的ID "+orderProjectPO.getId());
        //存orderProjectPO 在t_order_project表有一个orderId 需要保存order后生成的自增主键
        orderProjectPOMapper.insert(orderProjectPO);

        //保存筹集到的金额到t_project
        Double orderAmount = orderPO.getOrderAmount();

        String projectName = orderProjectPO.getProjectName();
        orderProjectPOMapper.updateProjectAmount(orderAmount,projectName);
    }
}
